/**
 * 
 */
package edu.hm.compiler.scanner.token;

import edu.hm.compiler.I18NCompiler;

/**
 * @author Saruman
 */
public class Token implements IToken
{

    private final ITokenClass tokenClass;
    private final int         line;
    private final int         charPos;
    private final String      pattern;

    /**
     * @param tokenClass
     * @param pattern
     * @param line
     * @param charPos
     */
    public Token( ITokenClass tokenClass, String pattern, int line, int charPos )
    {
        if( tokenClass == null )
            throw new IllegalArgumentException( I18NCompiler.getString("TokenClassNotNull") ); //$NON-NLS-1$
        if( pattern == null )
            throw new IllegalArgumentException( I18NCompiler.getString("PatternStringNotNull") ); //$NON-NLS-1$
        this.tokenClass = tokenClass;
        this.line = line;
        this.charPos = charPos;
        this.pattern = pattern;
    }

    public Token( ITokenClass tokenClass, String pattern )
    {
        this( tokenClass, pattern, -1, -1 );
    }

    /*
     * (non-Javadoc)
     * @see edu.hm.compiler.token.IToken#getTokenClass()
     */
    @Override
    public ITokenClass getTokenClass()
    {
        return this.tokenClass;
    }

    /*
     * (non-Javadoc)
     * @see edu.hm.compiler.token.IToken#getPattern()
     */
    @Override
    public String getPattern()
    {
        return this.pattern;
    }

    /*
     * (non-Javadoc)
     * @see edu.hm.compiler.token.IToken#getLine()
     */
    @Override
    public int getLine()
    {
        return this.line;
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ( ( this.pattern == null ) ? 0 : this.pattern.hashCode() );
        result = prime * result + ( ( this.tokenClass == null ) ? 0 : this.tokenClass.hashCode() );
        return result;
    }

    @Override
    public boolean equals( Object obj )
    {
        if( this == obj )
            return true;
        if( obj == null )
            return false;
        if( getClass() != obj.getClass() )
            return false;
        Token other = (Token) obj;
        if( this.pattern == null )
        {
            if( other.pattern != null )
                return false;
        } else if( !this.pattern.equals( other.pattern ) )
            return false;
        if( this.tokenClass == null )
        {
            if( other.tokenClass != null )
                return false;
        } else if( !this.tokenClass.equals( other.tokenClass ) )
            return false;
        return true;
    }

    /*
     * (non-Javadoc)
     * @see edu.hm.compiler.token.IToken#getCharPos()
     */
    @Override
    public int getCharPos()
    {
        return this.charPos;
    }

    @Override
    public String toString()
    {
        return "Token [tokenClass=" + this.tokenClass + ", line=" + this.line + ", charPos=" + this.charPos //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                + ", pattern=" + this.pattern //$NON-NLS-1$
                + "]"; //$NON-NLS-1$
    }

}
